package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.ICountryDao;
import com.magicode.cms.entiy.base.Country;
import com.magicode.cms.service.ICountryService;

@Service("serviceCountry")
public class CountryServiceImpl extends BaseServiceImpl<Country, ICountryDao> implements ICountryService,UtilPath {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编号");
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql = " from "+Country.class.getName()+" "+entity_alias_name+" where 1 = 1";
		if(null!=querywhere && !querywhere.isEmpty()){
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get(field_title)+"%'";
			}
			if(querywhere.containsKey("code")){
				hql = hql+" and "+entity_alias_name+".code like '%"+querywhere.get("code")+"%'";
			}
			if(querywhere.containsKey("countryId")){
				hql = hql+" and "+entity_alias_name+".country.id='"+querywhere.get("countryId")+"'";
			}
//			if(querywhere.containsKey("eventTypeId")){
//				hql = hql+" and "+entity_alias_name+".eventType.id='"+querywhere.get("eventTypeId")+"'";
//			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public ICountryDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return countryDao;
	}

	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			hql = hql + " order by "+entity_alias_name+".orderCode desc";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Autowired
	@Qualifier("daoCountry")
	ICountryDao countryDao;

	public ICountryDao getCountryDao() {
		return countryDao;
	}

	public void setCountryDao(ICountryDao countryDao) {
		this.countryDao = countryDao;
	}

	@Override
	public List<Country> getCountries(Class cl) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> simpleSelect(Map<String, String> querywhere) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
